package com.develop.skwen.basemodule.list;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by skwen
 * on 18-5-30
 */

public abstract class BaseAdapter<D> extends RecyclerView.Adapter<BaseHolder> {

    private List<D> mDList;

    private BaseItemClickListener mBaseItemClickListener;

    public BaseAdapter(List<D> list){
        this.mDList = list;
    }

    @Override
    public BaseHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return getViewHolder(LayoutInflater.from(parent.getContext()),parent,viewType);
    }

    protected abstract BaseHolder getViewHolder(LayoutInflater inflater, ViewGroup parent, int viewType);

    @Override
    public void onBindViewHolder(BaseHolder holder, final int position) {
        onBindHolder(holder,position);
        holder.bind(getItem(position));

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mBaseItemClickListener != null){
                    mBaseItemClickListener.onItemClickListener(position,getItem(position));
                }
            }
        });

    }

    protected void onBindHolder(BaseHolder holder, int position){

    }

    @Override
    public int getItemCount() {
        return mDList.size();
    }

    protected D getItem(int position){
        return mDList == null? null:mDList.get(position);
    }

    public void setOnBaseItemClickListener(BaseItemClickListener baseItemClickListener){
        mBaseItemClickListener = baseItemClickListener;
    }
}
